package cn.iocoder.yudao.module.etl.controller.admin.dataset;

import cn.iocoder.yudao.module.etl.dal.dataobject.DatasetTable;
import cn.iocoder.yudao.module.etl.dal.dataobject.DatasetTableField;
import cn.iocoder.yudao.module.etl.dal.dataobject.DatasetTableIncrementalConfig;
import cn.iocoder.yudao.module.etl.dto.DataSetTableDTO;
import cn.iocoder.yudao.module.etl.dto.dataset.ExcelFileData;
import cn.iocoder.yudao.module.etl.dto.datasource.TableField;
import cn.iocoder.yudao.module.etl.request.dataset.DataSetTableRequest;
import cn.iocoder.yudao.module.etl.response.DataSetDetail;
import cn.iocoder.yudao.module.etl.service.dataset.DataSetTableService;
import cn.iocoder.yudao.module.etl.utils.ResultHolder;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author gin
 * @Date 2021/2/20 8:29 下午
 */
@Api(tags = "数据集：数据集表")
@ApiSupport(order = 50)
@RestController
@RequestMapping("etl/dataset/table")
public class DataSetTableController {
    @Resource
    private DataSetTableService dataSetTableService;


    @ApiOperation("更新")
    @PostMapping("update")
    public void save(@RequestBody DataSetTableRequest datasetTable) throws Exception {
        if (datasetTable.getType().equalsIgnoreCase("excel")) {
            dataSetTableService.saveExcel(datasetTable);
        } else {
            dataSetTableService.save(datasetTable);
        }
    }


    @ApiOperation("修改")
    @PostMapping("alter")
    public void alter(@RequestBody DataSetTableRequest request) throws Exception {
        dataSetTableService.alter(request);
    }

    @ApiOperation("删除")
    @PostMapping("delete/{id}")
    public void delete(@ApiParam(name = "id", value = "数据集ID", required = true) @PathVariable String id) throws Exception {
        dataSetTableService.delete(id);
    }

    @ApiOperation("查询")
    @PostMapping("list")
    public List<DataSetTableDTO> list(@RequestBody DataSetTableRequest dataSetTableRequest) {
        return dataSetTableService.list(dataSetTableRequest);
    }

    @ApiOperation("查询组")
    @PostMapping("listAndGroup")
    public ResultHolder listAndGroup(@RequestBody DataSetTableRequest dataSetTableRequest) {
        return ResultHolder.success(dataSetTableService.listAndGroup(dataSetTableRequest));
    }

    @ApiOperation("详息")
    @PostMapping("get/{id}")
    public DatasetTable get(@ApiParam(name = "id", value = "数据集ID", required = true) @PathVariable String id) {
        return dataSetTableService.get(id);
    }

    @ApiOperation("带权限查询")
    @PostMapping("getWithPermission/{id}")
    public ResultHolder getWithPermission(@PathVariable String id) {
        return ResultHolder.success(dataSetTableService.getWithPermission(id, null));
    }

    @ApiOperation("查询原始字段")
    @PostMapping("getFields")
    public ResultHolder getFields(@RequestBody DatasetTable datasetTable) throws Exception {
        return ResultHolder.success(dataSetTableService.getFields(datasetTable));
    }

    @ApiOperation("查询生成字段")
    @PostMapping("getFieldsFromDE")
    public ResultHolder getFieldsFromDE(@RequestBody DataSetTableRequest dataSetTableRequest) throws Exception {
        return ResultHolder.success(dataSetTableService.getFieldsFromDE(dataSetTableRequest));
    }

    @ApiOperation("查询预览数据")
    @PostMapping("getPreviewData/{page}/{pageSize}")
    public ResultHolder getPreviewData(@RequestBody DataSetTableRequest dataSetTableRequest, @PathVariable Integer page, @PathVariable Integer pageSize) throws Exception {
        return ResultHolder.success(dataSetTableService.getPreviewData(dataSetTableRequest, page, pageSize, null));
    }

    @ApiOperation("db数据库表预览数据")
    @PostMapping("dbPreview")
    public ResultHolder  getDBPreview(@RequestBody DataSetTableRequest dataSetTableRequest) throws Exception {
        return ResultHolder.success(dataSetTableService.getDBPreview(dataSetTableRequest));
    }

    @ApiOperation("根据sql查询预览数据")
    @PostMapping("sqlPreview")
    public ResultHolder getSQLPreview(@RequestBody DataSetTableRequest dataSetTableRequest) throws Exception {
        return dataSetTableService.getSQLPreview(dataSetTableRequest);
    }

    @ApiOperation("预览自定义数据数据")
    @PostMapping("customPreview")
    public ResultHolder customPreview(@RequestBody DataSetTableRequest dataSetTableRequest) throws Exception {
        return ResultHolder.success(dataSetTableService.getCustomPreview(dataSetTableRequest));
    }

    @ApiOperation("查询增量配置")
    @PostMapping("incrementalConfig")
    public ResultHolder incrementalConfig(@RequestBody DatasetTableIncrementalConfig datasetTableIncrementalConfig) throws Exception {
        return ResultHolder.success(dataSetTableService.incrementalConfig(datasetTableIncrementalConfig));
    }

    @ApiOperation("保存增量配置")
    @PostMapping("save/incrementalConfig")
    public void saveIncrementalConfig(@RequestBody DatasetTableIncrementalConfig datasetTableIncrementalConfig) throws Exception {
        dataSetTableService.saveIncrementalConfig(datasetTableIncrementalConfig);
    }

    @ApiOperation("数据集详息")
    @PostMapping("datasetDetail/{id}")
    public ResultHolder datasetDetail(@PathVariable String id) {
        return ResultHolder.success(dataSetTableService.getDatasetDetail(id));
    }

    @ApiOperation("excel上传")
    @PostMapping("excel/upload")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile"),
            @ApiImplicitParam(name = "tableId", value = "数据表ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "editType", value = "编辑类型", required = true, dataType = "Integer")
    })
    public ResultHolder excelUpload(@RequestParam("file") MultipartFile file, @RequestParam("tableId") String tableId, @RequestParam("editType") Integer editType) throws Exception {
        return ResultHolder.success(dataSetTableService.excelSaveAndParse(file, tableId, editType));
    }

    @ApiOperation("检测doris")
    @PostMapping("checkDorisTableIsExists/{id}")
    public ResultHolder checkDorisTableIsExists(@PathVariable String id) throws Exception {
        return ResultHolder.success(dataSetTableService.checkEngineTableIsExists(id));
    }

    @ApiOperation("搜索")
    @PostMapping("search")
    public ResultHolder search(@RequestBody DataSetTableRequest dataSetTableRequest) {
        return ResultHolder.success(dataSetTableService.search(dataSetTableRequest));
    }

    @ApiOperation("数据集同步表结构")
    @PostMapping("syncField/{id}")
    public ResultHolder syncDatasetTableField(@PathVariable String id) throws Exception {
        return ResultHolder.success(dataSetTableService.syncDatasetTableField(id));
    }

    @ApiOperation("关联数据集预览数据")
    @PostMapping("unionPreview")
    public ResultHolder unionPreview(@RequestBody DataSetTableRequest dataSetTableRequest) throws Exception {
        return ResultHolder.success(dataSetTableService.getUnionPreview(dataSetTableRequest));
    }

    @ApiOperation("根据数据集文件夹ID查询数据集名称")
    @PostMapping("/getDatasetNameFromGroup/{sceneId}")
    public ResultHolder getDatasetNameFromGroup(@PathVariable String sceneId) {
        return ResultHolder.success(dataSetTableService.getDatasetNameFromGroup(sceneId));
    }

    @ApiOperation("批量保存")
    @PostMapping("batchAdd")
    public ResultHolder batchAdd(@RequestBody List<DataSetTableRequest> datasetTable) throws Exception {
        return ResultHolder.success(dataSetTableService.batchInsert(datasetTable));
    }
}
